package com.example.acceledemo;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.SystemClock;
import android.util.Log;

public class AcceleSensor implements SensorEventListener {

	private SensorManager sManager;
	private final MapActivity mContext;
	private AcceleRoute acceleRoute=null;
	private long startTime;


	public AcceleSensor(MapActivity mContext) {

		this.mContext = mContext;
		
		sManager = (SensorManager) (this.mContext.getSystemService(MapActivity.SENSOR_SERVICE));
		
		//startTime = System.currentTimeMillis();
		setStartTime(SystemClock.currentThreadTimeMillis());
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {

	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		
			if (event.sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION)
			addValues(event);
	}
	
	private void addValues(SensorEvent event)
	{
		float[] values = event.values;
		//long actualTimeClock = System.currentTimeMillis();
		//long actualTimeClock = SystemClock.currentThreadTimeMillis();
		Node node = new Node(values, 0);
		if(acceleRoute!=null)
		acceleRoute.addNode(node);
		
	}
	
	
	public void stopUsingAccele()
	{
		Log.d("location", "stopUsingAccele()");
		sManager.unregisterListener(this);
	}
	
	public void startUsingAccele()
	{
		Log.d("location", "startUsingAccele()");
		sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION), SensorManager.SENSOR_DELAY_NORMAL);
	}

	public void setAcceleRoute(AcceleRoute acceleRoute) {
		
		this.acceleRoute=acceleRoute;
		
	}

	public AcceleRoute getAcceleRoute() {
		
		return acceleRoute;
	}

	public long getStartTime() {
		return startTime;
	}

	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}
	

}
